package com.dabai.iot.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dabai.common.core.domain.R;
import com.dabai.common.core.utils.Func;
import com.dabai.common.mybatis.core.page.PageQuery;
import com.dabai.common.mybatis.core.page.TableDataInfo;
import com.dabai.common.web.core.BaseController;
import com.dabai.iot.core.convert.ConsumerGroupConvert;
import com.dabai.iot.core.dto.ConsumerGroupInput;
import com.dabai.iot.core.dto.ConsumerGroupUpdate;
import com.dabai.iot.core.entity.ConsumerGroupEntity;
import com.dabai.iot.core.entity.ServerSubscribeEntity;
import com.dabai.iot.core.service.IConsumerGroupService;
import com.dabai.iot.core.service.IServerSubscribeService;
import com.dabai.iot.core.vo.ConsumerGroupVO;
import com.dabai.iot.core.vo.ServerSubscribeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 消费组管理 控制器
 *
 * @author kev1n
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/consumer")
public class ConsumerGroupController extends BaseController {

    private final IConsumerGroupService consumerGroupService;
    private final IServerSubscribeService serverSubscribeService;

    /**
     * 新增消费组
     */
    @PostMapping("/add")
    @Operation(summary = "新增消费组")
    public R<Void> add(@RequestBody ConsumerGroupInput input) {
        return toAjax(consumerGroupService.addConsumer(input));
    }

    /**
     * 修改消费组
     */
    @PostMapping("/update")
    @Operation(summary = "修改消费组")
    public R<Void> update(@RequestBody ConsumerGroupUpdate input) {
        return toAjax(consumerGroupService.updateConsumer(input));
    }

    /**
     * 删除消费组
     */
    @PostMapping("/remove")
    @Operation(summary = "删除消费组", description = "传入主键id")
    public R<Void> remove(@Parameter(description = "主键id", required = true) @RequestParam Long id) {
        return toAjax(consumerGroupService.deleteConsumer(id));
    }

    /**
     * 消费组详情
     */
    @GetMapping("/detail")
    @Operation(summary = "消费组详情", description = "消费组详情，传入消费组主键Id")
    public R<ConsumerGroupVO> detail(@RequestParam Long id) {
        ConsumerGroupEntity consumerGroup = consumerGroupService.getById(id);
        if (Func.isNotEmpty(consumerGroup)) {
            ConsumerGroupVO detail = ConsumerGroupConvert.INSTANCE.convertVO(consumerGroup);

            String subscribes = serverSubscribeService.getListByConsumerId(id)
                .stream()
                .map(ServerSubscribeEntity::getId)
                .map(String::valueOf)
                .collect(Collectors.joining(","));
            detail.setSubscribes(subscribes);

            return R.ok(detail);
        }

        return R.ok(null);
    }

    /**
     * 消费组分页
     */
    @GetMapping("/page")
    @Operation(summary = "消费组分页", description = "分页，传入对应查询参数")
    public TableDataInfo<ConsumerGroupEntity> page(PageQuery pageQuery, ConsumerGroupEntity consumerGroup) {
        IPage<ConsumerGroupEntity> pages = consumerGroupService.getConsumerGroupPage(pageQuery.build(), consumerGroup);
        return TableDataInfo.build(pages);
    }

    /**
     * 根据消费组主键Id获取关联的订阅列表
     */
    @GetMapping("/subscribeList")
    @Operation(summary = "根据消费组主键Id获取关联的订阅列表", description = "根据消费组主键Id获取关联的订阅列表，传入消费组主键Id")
    public R<List<ServerSubscribeVO>> subscribeList(@Parameter(description = "消费组主键id", required = true) @RequestParam Long consumerId) {
        List<ServerSubscribeVO> voList = serverSubscribeService.getListByConsumerId(consumerId);
        return R.ok(voList);
    }

    /**
     * 解除订阅
     */
    @PostMapping("/unsubscription")
    @Operation(summary = "解除消费组和产品订阅关联", description = "解除消费组和产品订阅关联，至少保留一个订阅")
    public R<Void> unsubscription(@Parameter(description = "消费组主键id", required = true) @RequestParam Long consumerId,
                                  @Parameter(description = "产品订阅id", required = true) @RequestParam Long serverSubscribeId) {
        return toAjax(consumerGroupService.unsubscription(consumerId, serverSubscribeId));
    }

}
